#lang racket

;;By Spencer Hale

(require "Helper.scm")

(provide solveElimination)

;;runs through elimination functions
(define solveElimination
  (lambda (matrix possvec)
    (solveEliminationRow matrix possvec 0 1)
    (solveEliminationColumn matrix possvec 0 1))
  ;    (solveEliminationBox matrix possvec 0 0))
  )

(define solveEliminationRow
  (lambda (matrix possvec row val)  ; takes in specified row and value to look for (starts and 1 and cycles through to 9)
    (cond ((equal? (vector-count (rowHasPoss? val) (vector-ref possvec row)) 1)
           ;executes if there is only one position in the row where the value is possible
           (setRowElimination matrix possvec row val 0)))
    (if (equal? val 9)
        (if (equal? row 8) void ;if all rows have been tested for all values, returns void, otherwise moves along.
            (solveEliminationRow matrix possvec (+ row 1) 1)) 
        (solveEliminationRow matrix possvec row (+ val 1)))))

(define setRowElimination ; cycles through each position in the row until it finds the one that had the value as possible, and then sets the position
  (lambda (matrix possvec row val num)
    (if ((rowHasPoss? val) (vector-ref (vector-ref possvec row) num)) ;finds the position in the row that has the value as possible
        (setpos matrix possvec row num val) ;sets the position to the given value
        (if (equal? num 8) (and (write "setRowError") (newline))  ; this should never be reached
            (setRowElimination matrix possvec row val (+ num 1))))))

(define solveEliminationColumn ;takes specified column and value to look for
  (lambda (matrix possvec col val)
    (cond ((equal? (vector-count (colHasPoss? val col) possvec) 1) ;counts how many positions in the column have the value set as possible. If it equals 1, executes the following
           (setColElimination matrix possvec col val 0)
           ))
    (if (equal? val 9)
        (if (equal? col 8) void
            (solveEliminationColumn matrix possvec (+ col 1) 1))
        (solveEliminationColumn matrix possvec col (+ 1 val))
        )))

(define setColElimination ;cycles through until it finds the single position that had the possibility
  (lambda (matrix possvec col val num)
    (if ((colHasPoss? val col) (vector-ref possvec num)) ;checks if the position in the column has the possibility
        (setpos matrix possvec num col val) ; if it does, sets the position.
        (if (equal? num 8) (and (write "SetCol Error") (newline)); this should never bee reached
            (setColElimination matrix possvec col val (+ num 1))
            ))))

